<?php

namespace app\common\model;

/**
 * 订单商品模型
 * Class OrderGoods
 * @package app\common\model
 */
class OrderGoods extends BaseModel
{
    protected $name = 'order_goods';
    protected $updateTime = false;

    public static function getExpressList($order_id)
    {
        $list = db('order_express express')->join('order_goods goods','FIND_IN_SET(goods.`order_goods_id`,express.`order_goods_ids`)','left')
            ->field("express.`order_express_id`,GROUP_CONCAT(goods.`goods_name` SEPARATOR '、') goods_name,express.express_company,express.express_price")
            ->field("express.`express_no`,express.`delivery_time`,express.`delivery_status`,IF(express.delivery_status = 10,'未发货','已发货') delivery_status_text")
            ->where('express.order_id',$order_id)
            ->group('express.order_express_id')
            ->order('express.delivery_time asc')
            ->select();
        return $list;
    }

    /**
     * 订单商品列表
     * @return \think\model\relation\BelongsTo
     */
    public function image()
    {
        $model = "app\\common\\model\\UploadFile";
        return $this->belongsTo($model, 'image_id', 'file_id');
    }

    /**
     * 关联商品表
     * @return \think\model\relation\BelongsTo
     */
    public function goods()
    {
        return $this->belongsTo('Goods');
    }

    /**
     * 关联商品sku表
     * @return \think\model\relation\BelongsTo
     */
//    public function sku()
//    {
//        return $this->belongsTo('GoodsSku', 'spec_sku_id', 'spec_sku_id');
//    }

    /**
     * 关联订单主表
     * @return \think\model\relation\BelongsTo
     */
    public function orderM()
    {
        return $this->belongsTo('Order');
    }

    /**
     * 售后单记录表
     * @return \think\model\relation\HasOne
     */
    public function refund()
    {
        return $this->hasOne('OrderRefund');
    }

    /**
     * 订单商品详情
     * @param $where
     * @return OrderGoods|null
     * @throws \think\exception\DbException
     */
    public static function detail($where)
    {
        return static::get($where, ['image', 'refund']);
    }

}
